﻿using Microsoft.Extensions.Configuration;
using MJTop.Data;
using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Data;
using Wudi.Common.Extensions;
using System.Collections;

namespace ConsoleApp
{
    class Program
    {
        public static string ms_conn_str, mysql_conn_str, ora_conn_str, pg_conn_str, sqlite_conn_str;
        public static Dictionary<DBType, string> dc_type_str = new Dictionary<DBType, string>();
        static Program()
        {
            var config = new ConfigurationBuilder().SetBasePath(Environment.CurrentDirectory)
           .AddJsonFile("app.json")
           .Build();
            ms_conn_str = config[DBType.SqlServer.ToString()];
            mysql_conn_str = config[DBType.MySql.ToString()];
            ora_conn_str = config[DBType.Oracle.ToString()];
            pg_conn_str = config[DBType.PostgreSql.ToString()];
            sqlite_conn_str = config[DBType.SQLite.ToString()];

            dc_type_str = new Dictionary<DBType, string>()
            {
                { DBType.SqlServer, ms_conn_str },
                { DBType.MySql, mysql_conn_str },
                { DBType.Oracle, ora_conn_str },
                { DBType.PostgreSql, pg_conn_str },
                { DBType.SQLite, sqlite_conn_str },
            };
        }

        static void Main(string[] args)
        {
            var dbTypes = EnumInfo<DBType>.IntMembers.Values;

            foreach (var db_type in dbTypes)
            {
                var db = DBMgr.UseDB(db_type, dc_type_str[db_type]);

                var info = db.Info;
            }

            //var db = DBMgr.UseDB(DBType.MySql, dc_type_str[DBType.MySql]);
            //var info = db.Info;

            //info.Tools.GenerateEntityCode(@"E:\Models", "Wudi.Entity", false);


            var mysql = DBMgr.UseDB(DBType.MySql, "Data Source=51try.top;port=3300;Initial Catalog=star_tos;User ID=root;password=lzmj115.;Connect Timeout=30;Pooling=true;Max Pool Size=512;");
            var data = mysql.GetDataTable("select * from sys_config");

            var dt = new DataTable();
            dt.AddColumns(new KeyValuePair<string, Type>[] {
                new KeyValuePair<string, Type>("id",typeof(string)),
                new KeyValuePair<string, Type>("tit",typeof(string)),
                new KeyValuePair<string, Type>("type",typeof(int)),
                new KeyValuePair<string, Type>("key",typeof(string)),
                new KeyValuePair<string, Type>("hash_field",typeof(string)),
                new KeyValuePair<string, Type>("value",typeof(string)),
                new KeyValuePair<string, Type>("show_type",typeof(string)),
                new KeyValuePair<string, Type>("addeduid",typeof(long)),
                //new KeyValuePair<string, Type>("id",typeof(string)),
                //new KeyValuePair<string, Type>("id",typeof(string)),
                //new KeyValuePair<string, Type>("id",typeof(string)),
                //new KeyValuePair<string, Type>("id",typeof(string)),
            });

            foreach (DataRow dr in data.Rows)
            {
                var dr_new = dt.NewRow();
                dr_new.ItemArray = new object[]
                {
                    dr["id"].ToString(),
                    dr["title"].ToString(),
                    dr["type"].ToString(),
                    dr["section"].ToString(),
                    dr["keyname"].ToString(),
                    dr["value"].ToString(),
                    "text",
                    dr["addeduid"].ToString(),
                };
                dt.Rows.Add(dr_new);
            }

            //int res = db.BulkInsert("sys_config", dt);


            var lst = dt.ConvertToListObject<sys_config>();

            //foreach (DataRow dr in dt.Rows)
            //{
            //    db.Insert(dr.ToDictionary(), "sys_config");
            //}

          

        }
    }
}
